<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="角色名称"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入角色名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="描述"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-textarea
            v-decorator="['description', {}]"
            placeholder="请输入描述"
            :auto-size="{ minRows: 3, maxRows: 5 }"
          />
        </a-form-item>
        <a-form-item
          label="管理员"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-checkbox 
            :checked="checked"
            v-decorator="['type', {}]"
            @change="onChange">
            拥有全部权限
          </a-checkbox>
        </a-form-item>
        <a-form-item
          label="所有模块"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <div v-if="treeData && treeData.length">
            <template>
              <a-tree
                checkable
                :defaultExpandAll="true"
                :tree-data="treeData"
                :checked-keys="checkedKeys"
                @check="onCheck"
               
              />
            </template>
          </div>
        </a-form-item>
         
         
      </a-form>
    </a-spin>
  </a-modal>
</template>


<script>
import pick from 'lodash.pick'
import { Tree } from 'ant-design-vue'
  import {
    postAxiosUrl,
    getUrl
  } from '@/api/manage'
export default {
components:{ ATree  :Tree },
  data () {
    return {
      id : '',
      checked : false,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      treeData : [],
      expandedKeys :[],
      checkedKeys  :[],
    }
  },
  created () {
     this.fetch()
  },
  methods: {
  add () {
    this.id = ''
    this.form.resetFields()
    this.visible = true
    this.checked = false
    this.checkedKeys = []
   
  },
  edit (record) {
     const { form: { setFieldsValue } } = this
     this.visible = true
     const me = this
      this.$nextTick(() => {
      me.id = record.id
      setFieldsValue(pick(record, ['name']))
      setFieldsValue(pick(record, ['description']))
       if(record.type == '1'){
         me.checked = true
         this.checkedKeys = [0]
       }else{
         me.checked = false
         me.fetchPermission()
       }
    })
    
  },
  close () {
      this.$emit('close')
      this.visible = false
  },
  handleCancel () {
      this.close()
  },
  onChange(e) {
      this.checked = e.target.checked
      if(this.checked == true)
        this.checkedKeys = [0]
      else
         this.checkedKeys = []
  },
  fetch(params = {}) {
    console.log('params:', params)
    this.loading = true
    var _this = this
    return getUrl('getAllPermission', Object.assign(params, this.queryParam))
      .then(data => {
       
        this.treeData = data.result
        this.expandedKeys = [0]
        
      })
  },
  fetchPermission(params = {}) {
    console.log('params:', params)
    this.loading = true
    var _this = this
    return getUrl('getPermission', {id : this.id})
      .then(data => {
        this.checkedKeys = data.result
      })
  },
  handleOk () {
      const _this = this
      
      // 触发表单验证
      this.form.validateFields((err, values) => {
        // 验证表单没错误
        if (!err) {
          console.log('form values', values)
          _this.confirmLoading = true
         values.id = this.id
         values.type = this.checked ? '1' : '0'
         values.permissions = this.checkedKeys
         postAxiosUrl('setRole', values)
           .then(data => {
              if(!data.success){
                 _this.$message.warning(data.error)
                 _this.confirmLoading = false
               }else{
                 _this.visible = false
                 _this.confirmLoading = false
                 _this.$emit('ok', values)
               }           
             })
        }
      })
    },
    onExpand(expandedKeys) {
          console.log('onExpand', expandedKeys)
          // if not set autoExpandParent to false, if children expanded, parent can not collapse.
          // or, you can remove all expanded children keys.
          this.expandedKeys = expandedKeys
        },
        onCheck(checkedKeys, obj) {
          const parentId =checkedKeys.filter(item=> item == 0)
          if(parentId && parentId[0] == 0){
             this.checked = true
          }else{
            this.checked = false
          }
          this.checkedKeys = checkedKeys
        },
  }
}
</script>

<style>
</style>
